﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DrivingGame
{
    class GameObject
    {
        public static GraphicsDevice device;
        public static ContentManager Content;

        public float scale;
        public Vector3 position { get; set; }
        public Quaternion rotation;

        public Model model;

        public GameObject()
        {
            scale = 1.0f;
            position = new Vector3(0.0f, 0.0f, 0.0f);
            rotation = Quaternion.Identity;
        }

        public void LoadModel(string assetName)
        {
            model = Content.Load<Model>(assetName);
        }



        public void Draw(Matrix view, Matrix projection)
        {
            Matrix worldMatrix = Matrix.CreateScale(scale) * Matrix.CreateFromQuaternion(rotation) * Matrix.CreateTranslation(position);

            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;
                    effect.View = view;
                    effect.Projection = projection;
                }
                mesh.Draw();
            }
        }

        public void DrawCar(Matrix view, Matrix projection)
        {
            Matrix worldMatrix = Matrix.CreateScale(scale) * Matrix.CreateFromQuaternion(rotation) * Matrix.CreateTranslation(position);

            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (Effect effect in mesh.Effects)
                {
                    effect.Parameters["world"].SetValue(transforms[mesh.ParentBone.Index] * worldMatrix);
                    effect.Parameters["viewProj"].SetValue(view * projection);
                    effect.Parameters["viewInverse"].SetValue(Matrix.Invert(view));
                }
                mesh.Draw();
            }
        }
    }
}
